package com.mjk.manager.task.work;

import com.mjk.common.base.constns.BeeCommonConstns;
import com.mjk.common.base.constns.BeeStatus;
import com.mjk.common.base.error.BeeError;
import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.base.work.BaseWork;
import com.mjk.common.base.work.annotation.BeeWork;
import com.mjk.common.core.jdbc.bean.BeePage;
import com.mjk.common.core.jdbc.service.plus.idbservice.BeeDBService;
import com.mjk.common.core.jdbc.service.plus.wrap.BeeQueryWrapper;
import com.mjk.common.core.redis.datatype.RedisUtils;
import com.mjk.common.tools.scheduled.DynamicScheduledModel;
import com.mjk.common.tools.scheduled.single.SinglePointInterface;
import com.mjk.common.util.ObjectUtils;
import com.mjk.manager.api.mapper.ApiMapper;
import com.mjk.manager.task.conf.TaskConf;
import com.mjk.manager.task.model.TaskModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@BeeWork(work="task",isScheduled = true)
public class TaskWork extends BaseWork {
	@Autowired
	private BeeDBService beeDBService;
	@Autowired
	private TaskConf taskConf;
	
	@Autowired
	private RedisTemplate redisTemplate;
	
	@Autowired
	private ApiMapper mapper;

	
	public BeeResponse getAllTasks(TaskModel dto){
		BeeQueryWrapper wrapper = new BeeQueryWrapper(dto);
		if(ObjectUtils.isNotEmpty(dto.getTaskName())) {
			wrapper.like(TaskModel::getTaskName,dto.getTaskName());
		}
		BeePage<TaskModel> page = beeDBService.findManyByWrapperWithPage(wrapper);
		String nodeName = (String) RedisUtils.get(redisTemplate, SinglePointInterface.ROOT_PATH);
		for(TaskModel model :page.getList()){
			model.setRunName(nodeName);
		}
		return BeeError.BEE_SUC.tranceError(page);
	}
	
	public BeeResponse updateTask(TaskModel model){
		needParams(model, TaskModel::getId);
		BeeQueryWrapper wrapper = new BeeQueryWrapper(model);
		wrapper.eq(TaskModel::getId,model.getId());
		beeDBService.update(wrapper);
		for(DynamicScheduledModel ds : taskConf.getTasks()) {
			if(ds.getId().equals(model.getId()+"")) {
				ds.setCron(ObjectUtils.isEmpty(model.getCorn())?ds.getCron():model.getCorn());
				ds.setValid(ObjectUtils.isEmpty(model.getStatus())?ds.isValid(): BeeStatus.RUNNING.getCode().equals(model.getStatus()));
				ds.setTaskInstance(ObjectUtils.isEmpty(model.getTaskInstance())?ds.getTaskInstance():model.getTaskInstance());
				ds.setTaskMethod(ObjectUtils.isEmpty(model.getTaskMethod())?ds.getTaskMethod():model.getTaskMethod());
				ds.setTaskParams(ObjectUtils.isEmpty(model.getTaskParams())?ds.getTaskParams():model.getTaskParams());
				ds.setExt(ObjectUtils.isEmpty(model.getTaskContent())?ds.getExt():model.getTaskContent());
			}
		}
		return BeeError.BEE_SUC.tranceError("");
	}
	/**
	 * 保存任务
     */
	public BeeResponse saveTask(TaskModel model) {
		needParams(model, "taskName","corn");
		Pattern choosePattern = Pattern.compile(BeeCommonConstns.CORN_MATCH, Pattern.DOTALL);
		Matcher matcher = choosePattern.matcher(model.getCorn());
		if(!matcher.find()) {
			 return BeeError.SYSTEM_ERROR_CORN_ERROR.tranceError();
		}
		if(ObjectUtils.isEmpty(model.getTaskInstance())) {
			model.setTaskInstance("");
		}
		if(ObjectUtils.isEmpty(model.getTaskMethod())) {
			model.setTaskMethod("");
		}
		if(ObjectUtils.isEmpty(model.getTaskParams())) {
			model.setTaskParams("");
		}
		beeDBService.insert(model);
		taskConf.getTasks().add(DynamicScheduledModel.builder()
				.id(model.getId()+"")
				.taskInstance(model.getTaskInstance())
				.taskMethod(model.getTaskMethod())
				.taskParams(model.getTaskParams())
				.cron(model.getCorn())
				.type(model.getTaskType())
				.isValid(BeeStatus.RUNNING.getCode().equals(model.getStatus()))
				.build());
		return BeeError.BEE_SUC.tranceError("");
	}
}
